package com.blackhorse.anlis.anli_tx_aop;


import com.blackhorse.utils.TransactionManager;
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * 使用Spring Aop完成之前转账案例中事务控制
 */
public class EnableTransactionWithArroudAop {
    private TransactionManager transactionManager;

    public void setTransactionManager(TransactionManager transactionManager) {
        this.transactionManager = transactionManager;
    }

    public Object arroundTransaction(ProceedingJoinPoint pjp){
        Object rtValue = null;
        try {
            transactionManager.beginTransaction();
            Object[] args = pjp.getArgs();
            rtValue = pjp.proceed(args);
            transactionManager.commit();
            return rtValue;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            transactionManager.rollback();
            return null;
        }finally {
            transactionManager.release();
        }
    }

}
